﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TC.SSO.Server
{
    public class SitesConfig
    {
        public string Url { get; set; }

        public string Remark { get; set; }

        static readonly string ConfigPath = string.Empty;

        static readonly string ConfigCacheKey = string.Empty;

        static SitesConfig()
        {
            ConfigPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                @"Config\SitesConfig.xml");
            ConfigCacheKey = typeof(SitesConfig).GetHashCode().ToString();
        }
        
        /// <summary>
        /// 获取使用单点系统的各个网站的退出地配置
        /// </summary>
        /// <returns></returns>
        public static List<SitesConfig> GetConfig()
        {
            var sitesConfig = HttpRuntime.Cache[ConfigCacheKey] as List<SitesConfig>;
            if (sitesConfig == null)
            {
                try
                {
                    sitesConfig = new List<SitesConfig>();
                    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                    doc.Load(ConfigPath);
                    var siteNodes = doc.SelectNodes("sites/site");
                    foreach (System.Xml.XmlNode node in siteNodes)
                    {
                        sitesConfig.Add(new SitesConfig
                        {
                            Url = node.Attributes["url"].Value,
                            Remark = node.Attributes["remark"].Value
                        });
                    }
                    System.Web.Caching.CacheDependency cd = new System.Web.Caching.CacheDependency(ConfigPath);
                    HttpRuntime.Cache.Insert(ConfigCacheKey, sitesConfig, cd);
                }
                catch (Exception ex)
                {
                    //todo:日志记录
                }
            }
            return sitesConfig;
        }
    }

    public class PageUrlConfig
    {
        public readonly static string BuildNetRegisterUrl = null;
        public readonly static string BuildNetForgotPasswordUrl = null;
        public readonly static string EBuildNetSite = null;
        public readonly static string BuildNetSite = null;
        static PageUrlConfig()
        {
            BuildNetRegisterUrl = System.Configuration.ConfigurationManager.AppSettings["page.buildnet.register"];
            BuildNetForgotPasswordUrl = System.Configuration.ConfigurationManager.AppSettings["page.buildnet.forgotpassword"];
            EBuildNetSite = System.Configuration.ConfigurationManager.AppSettings["eBulletinUrl"];
            BuildNetSite = System.Configuration.ConfigurationManager.AppSettings["BuildnetUrl"];
        }
    }
}